package com.zrrd.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.common.result.Result;
import com.zrrd.model.product.*;
import com.zrrd.product.service.SkuInfoService;
import com.zrrd.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SkuInfoService skuInfoService;


    //查询列表 品牌
    @GetMapping("{page}/{limit}")
    public Result<Page> page(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit, Long category3Id) {
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);
        QueryWrapper<SpuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);
        spuInfoService.page(spuInfoPage, queryWrapper);
        return Result.ok(spuInfoPage);


    }

    //品牌添加
    @PostMapping("/saveSpuInfo")
    public Result<String> savep(@RequestBody SpuInfo spuInfo) {
        spuInfoService.saveSpu(spuInfo);
        return Result.ok("保存成功");
    }

    //删除品牌
    @DeleteMapping("/remove/{id}")
    public Result<String> delete(@PathVariable("id") Integer id) {
        boolean byId = spuInfoService.removeById(id);
        if (byId) {
            return Result.ok("删除成功");
        }
        {
            return Result.fail("删除失败");
        }


    }

    //修改品牌
    @PutMapping("/update")
    public Result<String> update(@RequestBody BaseTrademark baseTrademark) {
        //spuInfoService.updateBatchById(baseTrademark);
        return Result.ok("删除成功");


    }

    //加载spu图片
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId) {

        return Result.ok(spuInfoService.getSpuImage(spuId));
    }

    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId) {
        return Result.ok(spuInfoService.SpuSaleAttrList(spuId));
    }


    //sku保存
    @PostMapping("/saveSkuInfo")
    public Result<String> saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok("添加成功");
    }


}
